home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / Sources / FWRgnShp.cpp < prev    next >
Encoding:
Text File  |  1996-04-25  |  11.6 KB  |  430 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWRgnShp.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWRGNSHP_H
  13. #include "FWRgnShp.h"
  14. #endif
  15.  
  16. #ifndef SLGRGLOB_H
  17. #include "SLGrGlob.h"
  18. #endif
  19.  
  20. #ifndef FWGC_H
  21. #include "FWGC.h"
  22. #endif
  23.  
  24. #ifndef FWGRUTIL_H
  25. #include "FWGrUtil.h"
  26. #endif
  27.  
  28. #ifndef FWFXMATH_H
  29. #include "FWFxMath.h"
  30. #endif
  31.  
  32. #ifndef SLRENDER_H
  33. #include "SLRender.h"
  34. #endif
  35.  
  36. #ifndef SLREGION_H
  37. #include "SLRegion.h"
  38. #endif
  39.  
  40. #ifndef FWODGEOM_H
  41. #include "FWODGeom.h"
  42. #endif
  43.  
  44. #ifndef FWSOMENV_H
  45. #include "FWSOMEnv.h"
  46. #endif
  47.  
  48. // ----- Foundation Includes -----
  49.  
  50. #ifndef FWSTREAM_H
  51. #include "FWStream.h"
  52. #endif
  53.  
  54. // ----- OpenDoc Includes -----
  55.  
  56. #ifndef SOM_ODTransform_xh
  57. #include <Trnsform.xh>
  58. #endif
  59.  
  60. #ifndef FW_BUILD_WIN
  61. #ifndef SOM_Module_OpenDoc_Polygon_defined
  62. #include <Polygon.xh>
  63. #endif
  64. #endif
  65.  
  66. //========================================================================================
  67. // File scope definitions
  68. //========================================================================================
  69.  
  70. #if defined(__MWERKS__) && GENERATING68K
  71. // A hack to work around a bug
  72. #pragma import list somGetGlobalEnvironment, somNewObjectInstance
  73. #endif
  74.  
  75. #ifdef FW_BUILD_MAC
  76. #pragma segment FWGraphics_RegionShape
  77. #endif
  78.  
  79. //========================================================================================
  80. //    class FW_CRegionShape
  81. //========================================================================================
  82.  
  83. FW_DEFINE_AUTO(FW_CRegionShape)
  84. FW_DEFINE_CLASS_M1(FW_CRegionShape, FW_CShape)
  85.  
  86. // This class is archivable, but we provide the archiving implementation in a separate
  87. // translation unit in order to enable deadstripping of the archiving-related code
  88. // in parts that do not use archiving with this class.
  89.  
  90. //----------------------------------------------------------------------------------------
  91. //    FW_CRegionShape::FW_CRegionShape
  92. //----------------------------------------------------------------------------------------
  93.  
  94. FW_CRegionShape::FW_CRegionShape(ODShape* odShape,
  95.                                  FW_ERenderVerbs renderVerb,
  96.                                  const FW_CInk& ink,
  97.                                  const FW_CStyle& style) :
  98.     FW_CShape(renderVerb, ink, style, FW_kNormalFont),
  99.     fODShape(odShape)
  100. {
  101.     FW_END_CONSTRUCTOR
  102. }
  103.  
  104. //----------------------------------------------------------------------------------------
  105. //    FW_CRegionShape::FW_CRegionShape
  106. //----------------------------------------------------------------------------------------
  107.  
  108. FW_CRegionShape::FW_CRegionShape(const FW_CRegionShape& other) :
  109.     FW_CShape(other),
  110.     fODShape(NULL)
  111. {
  112.     if (other.fODShape != NULL)
  113.     {
  114.         FW_SOMEnvironment ev;
  115.         fODShape = other.fODShape->Copy(ev);
  116.     }
  117.         
  118.     FW_END_CONSTRUCTOR
  119. }
  120.  
  121. //----------------------------------------------------------------------------------------
  122. //    FW_CRegionShape::FW_CRegionShape
  123. //----------------------------------------------------------------------------------------
  124.  
  125. FW_CRegionShape::FW_CRegionShape(FW_CReadableStream& stream) :
  126.     FW_CShape(stream),
  127.     fODShape(NULL)
  128. {
  129. #ifndef FW_BUILD_WIN
  130.     unsigned long dataSize;
  131.     stream >> dataSize;
  132.     ODPolygon* polygon = (ODPolygon*)::operator new(dataSize);
  133.     stream.Read((char*)polygon, dataSize);
  134.     
  135.     FW_SOMEnvironment ev;
  136.     // [B1 Conversion] Should diseapear when region shape doesn't use ODShape
  137.     fODShape = new ODShape;
  138.     fODShape->SetPolygon(ev, polygon);
  139. #else
  140.     FW_DEBUG_MESSAGE("Not Yet Implemented");
  141. #endif
  142.  
  143.     FW_END_CONSTRUCTOR
  144. }
  145.  
  146. //----------------------------------------------------------------------------------------
  147. //    FW_CRegionShape::~FW_CRegionShape
  148. //----------------------------------------------------------------------------------------
  149.  
  150. FW_CRegionShape::~FW_CRegionShape()
  151. {
  152.     FW_START_DESTRUCTOR
  153.     FW_SOMEnvironment ev;
  154.     DisposeODShape(ev);
  155. }
  156.  
  157. //----------------------------------------------------------------------------------------
  158. //    FW_CRegionShape::operator=
  159. //----------------------------------------------------------------------------------------
  160.  
  161. FW_CRegionShape& FW_CRegionShape::operator=(const FW_CRegionShape& other)
  162. {
  163.     if (this != &other)
  164.     {
  165.         FW_CShape::operator=(other);
  166.         
  167.         FW_SOMEnvironment ev;
  168.         DisposeODShape(ev);
  169.         fODShape = other.fODShape->Copy(ev);
  170.     }
  171.     
  172.     return *this;
  173. }
  174.  
  175. //----------------------------------------------------------------------------------------
  176. //    FW_CRegionShape::Render
  177. //----------------------------------------------------------------------------------------
  178.  
  179. void FW_CRegionShape::Render(FW_CGraphicContext& gc) const
  180. {
  181.     FW_ASSERT(fODShape != NULL);
  182.  
  183.     FW_PrivRenderRegion(gc.GetEnvironment(),
  184.         gc,
  185.         fODShape,
  186.         GetRenderVerb(),
  187.         fInk,
  188.         fStyle);
  189.     FW_FailOnEvError(gc.GetEnvironment());
  190. }
  191.  
  192. //----------------------------------------------------------------------------------------
  193. //    FW_CRegionShape::Render
  194. //----------------------------------------------------------------------------------------
  195.  
  196. void FW_CRegionShape::RenderRegion(FW_CGraphicContext& gc,
  197.                                     ODShape* odShape, 
  198.                                     FW_ERenderVerbs renderVerb, 
  199.                                     const FW_CInk& ink,
  200.                                     const FW_CStyle& style)
  201. {
  202.     FW_ASSERT(odShape != NULL);
  203.     
  204.     FW_PrivRenderRegion(gc.GetEnvironment(),
  205.         gc,
  206.         odShape,
  207.         renderVerb,
  208.         ink,
  209.         style);
  210.     FW_FailOnEvError(gc.GetEnvironment());
  211. }
  212.  
  213. //----------------------------------------------------------------------------------------
  214. //    FW_CRegionShape::AdoptODShape
  215. //----------------------------------------------------------------------------------------
  216.  
  217. void FW_CRegionShape::AdoptODShape(ODShape* odShape)
  218. {
  219.     FW_ASSERT(odShape != NULL);
  220.     
  221.     FW_SOMEnvironment ev;
  222.     DisposeODShape(ev);
  223.     fODShape = odShape;    
  224. }
  225.  
  226. //----------------------------------------------------------------------------------------
  227. //    FW_CRegionShape::OrphanODShape
  228. //----------------------------------------------------------------------------------------
  229.  
  230. ODShape* FW_CRegionShape::OrphanODShape()
  231. {
  232.     ODShape* odTempShape = fODShape;
  233.     fODShape = NULL;
  234.     return odTempShape;
  235. }
  236.  
  237. //----------------------------------------------------------------------------------------
  238. //    FW_CRegionShape::SetGeometry
  239. //----------------------------------------------------------------------------------------
  240.  
  241. void FW_CRegionShape::SetGeometry(ODShape* odShape)
  242. {
  243.     FW_SOMEnvironment ev;
  244.     DisposeODShape(ev);
  245.     fODShape = odShape->Copy(ev);
  246. }
  247.  
  248. //----------------------------------------------------------------------------------------
  249. //    FW_CRegionShape::DisposeODShape
  250. //----------------------------------------------------------------------------------------
  251.  
  252. void FW_CRegionShape::DisposeODShape(Environment* ev)
  253. {
  254.     if (fODShape != NULL)
  255.     {
  256.         fODShape->Release(ev);
  257.         fODShape = NULL;
  258.     }
  259. }
  260.  
  261. //----------------------------------------------------------------------------------------
  262. //    FW_CRegionShape::HitTest
  263. //----------------------------------------------------------------------------------------
  264.  
  265. FW_Boolean FW_CRegionShape::HitTest(FW_CGraphicContext& gc,
  266.                                     const FW_CPoint& test,
  267.                                     FW_Fixed tolerance) const
  268. {
  269.     if (fODShape == NULL)
  270.         return FALSE;
  271.  
  272.     if(fRenderVerb == FW_kNoRendering)
  273.         return FALSE;
  274.  
  275.     Environment* ev = gc.fEnvironment;
  276.  
  277.     ODRgnHandle rgn = ::FW_GetShapeRegion(ev, fODShape);
  278.  
  279.     FW_CRect testRect(test.x - tolerance, test.y - tolerance,
  280.                       test.x + tolerance, test.y + tolerance);
  281.     
  282.     if (!::FW_RectInRegion(rgn, testRect))
  283.         return FALSE;
  284.  
  285.     if (fRenderVerb == FW_kFrame)
  286.     {
  287.         ODShape* insideShape = fODShape->Copy(ev);
  288.         ::FW_OutlineODShape(ev, insideShape, GetPenSize());
  289.  
  290.         ODRgnHandle insideRgn = ::FW_GetShapeRegion(ev, insideShape);
  291.         
  292.         FW_Boolean isInside = ::FW_RectInRegion(insideRgn, testRect);
  293.  
  294.         insideShape->Release(ev);
  295.         
  296.         return !isInside;
  297.     }
  298.     
  299.     return TRUE;
  300. }
  301.  
  302. //----------------------------------------------------------------------------------------
  303. //    FW_CRegionShape::Copy
  304. //----------------------------------------------------------------------------------------
  305.  
  306. FW_CShape* FW_CRegionShape::Copy() const
  307. {
  308.     return FW_NEW(FW_CRegionShape, (*this));
  309. }
  310.  
  311. //----------------------------------------------------------------------------------------
  312. //    FW_CRegionShape::Flatten
  313. //----------------------------------------------------------------------------------------
  314.  
  315. void FW_CRegionShape::Flatten(FW_CWritableStream& stream) const
  316. {
  317.  
  318.     if (fODShape == NULL)
  319.         return;
  320.  
  321.     FW_DEBUG_MESSAGE("Not Yet Implemented");
  322.     FW_UNUSED(stream);
  323.  
  324. /*
  325. #ifndef FW_BUILD_WIN
  326.     ODPolygon* polygon = fODShape->CopyPolygon(somGetGlobalEnvironment());
  327.     
  328.     unsigned long dataSize = sizeof(ODSLong) + polygon->nContours * sizeof(ODSLong);
  329.     x
  330.     const ODContour *c = &polygon->firstContour;
  331.     for( ODSLong i = polygon->nContours; i>0; i-- ) {
  332.         dataSize += c->nVertices * sizeof(ODPoint);
  333.         c = (ODContour*)&c->vertex[c->nVertices];    // give the next contour
  334.     }
  335.  
  336.     stream << dataSize;
  337.     stream.Write((char*)polygon, dataSize);
  338.     
  339.     delete polygon;    // [HLX] can I delete that
  340. #else    
  341. FW_UNUSED(stream);
  342.     FW_DEBUG_MESSAGE("Not Yet Implemented");
  343. #endif
  344. */
  345. }
  346.  
  347. //----------------------------------------------------------------------------------------
  348. //    FW_CRegionShape::GetBounds
  349. //----------------------------------------------------------------------------------------
  350.  
  351. void FW_CRegionShape::GetBounds(FW_CGraphicContext& gc, FW_CRect& rect) const
  352. {
  353. FW_UNUSED(gc);
  354.  
  355.     fODShape->GetBoundingBox(gc.fEnvironment, (ODRect*) &rect);
  356. }
  357.  
  358. //----------------------------------------------------------------------------------------
  359. //    FW_CRegionShape::GetAnchorPoint
  360. //----------------------------------------------------------------------------------------
  361.  
  362. FW_CPoint FW_CRegionShape::GetAnchorPoint() const
  363. {
  364.     FW_CRect rect;
  365.     fODShape->GetBoundingBox(somGetGlobalEnvironment(), (ODRect*)&rect);
  366.     
  367.     return rect.TopLeft();
  368. }
  369.  
  370. //----------------------------------------------------------------------------------------
  371. //    FW_CRegionShape::Transform
  372. //----------------------------------------------------------------------------------------
  373.  
  374. void FW_CRegionShape::Transform(Environment* ev, ODTransform* odTransform)
  375. {
  376.     fODShape->Transform(ev, odTransform);
  377. }
  378.  
  379. //----------------------------------------------------------------------------------------
  380. //    FW_CRegionShape::InverseTransform
  381. //----------------------------------------------------------------------------------------
  382.  
  383. void FW_CRegionShape::InverseTransform(Environment* ev, ODTransform* odTransform)
  384. {
  385.     fODShape->InverseTransform(ev, odTransform);
  386. }
  387.  
  388. //----------------------------------------------------------------------------------------
  389. //    FW_CRegionShape::MoveShape
  390. //----------------------------------------------------------------------------------------
  391.  
  392. void FW_CRegionShape::MoveShape(FW_Fixed deltaX, FW_Fixed deltaY)
  393. {
  394.     Environment* ev = somGetGlobalEnvironment();    // [HLX] for now
  395.         
  396.     ODTransform* odTransform = ::FW_NewODTransform(ev, FW_CPoint(deltaX, deltaY));
  397.     fODShape->Transform(ev, odTransform);
  398.     odTransform->Release(ev);
  399. }
  400.  
  401. //----------------------------------------------------------------------------------------
  402. //    FW_CRegionShape::MoveShapeTo
  403. //----------------------------------------------------------------------------------------
  404.  
  405. void FW_CRegionShape::MoveShapeTo(FW_Fixed x, FW_Fixed y)
  406. {
  407.     FW_SOMEnvironment ev;
  408.  
  409.     FW_CRect box;
  410.     fODShape->GetBoundingBox(ev, (ODRect*) &box);
  411.     MoveShape(x - box.left, y - box.top);
  412. }
  413.  
  414. //----------------------------------------------------------------------------------------
  415. //    FW_CRegionShape::Inset
  416. //----------------------------------------------------------------------------------------
  417.  
  418. void FW_CRegionShape::Inset(FW_Fixed x, FW_Fixed y)
  419. {
  420.     FW_SOMEnvironment ev;
  421.  
  422.     ODRgnHandle plfmRgn = ::FW_CopyRegion(::FW_GetShapeRegion(ev, fODShape));
  423.     
  424.     ::FW_InsetRegion(plfmRgn, x, y);
  425.  
  426.     ::FW_SetShapeRegion(ev, fODShape, plfmRgn);
  427.     ::FW_DisposeRegion(plfmRgn);
  428. }
  429.  
  430.